package eerbaar.v2.effects;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EffectsView {

    private final long startTime = new Date().getTime();
    private final List<Effect> effects = new ArrayList<Effect>();
    private final EffectGenerator generator;

    public EffectsView() {
        this.generator = new EffectGenerator(this);
        this.generator.start();
    }

    public void addEffect(Effect effect) {

        if (!this.effects.contains(effect)) {
            this.effects.add(effect);
        }
    }

    public void draw(Graphics2D g) {

        // Graphics2D g = (Graphics2D) canvas.getBufferStrategy()
        // .getDrawGraphics();

        this.generator.check();

        final long current = new Date().getTime();
        final int age = (int) (current - this.startTime);

        List<Effect> removeList = new ArrayList<Effect>();

        for (Effect effect : this.effects) {

            if (effect.isExpired(age)) {
                removeList.add(effect);
                continue;
            }

            effect.display(age, g);
        }

        for (Effect effect : removeList) {
            removeEffect(effect);
        }
    }

    public void removeEffect(Effect effect) {
        if (this.effects.contains(effect)) {
            this.effects.remove(effect);
        }
    }

}
